package com.hex.ds.hdrs.log.service;

import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.common.utils.MapUtil;
import com.hex.ds.hdrs.log.dao.MailSendLogDao;
import com.hex.ds.hdrs.log.po.MailSendLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MailSendLogService {

    @Autowired
    private MailSendLogDao mailSendLogDao;

    /**
     * 查询所有的邮件发送记录信息
     *
     * @param mailSendLog
     * @param page
     * @return
     */
    public List<MailSendLog> qryMailSendLog(MailSendLog mailSendLog, Page page) {
        log.info("进入了【MailSendLogService】的qryMailSendLog方法");
        Map map = MapUtil.convertObjToMap(mailSendLog);
        return this.mailSendLogDao.qryMailSendLog(map, page);
    }

    public List<MailSendLog> qryNotSendLog() {
        Map paramMap = new HashMap(16);
        paramMap.put("sendStatus", Const.MAIL_NOT_SEND);
        return this.qryMailSendLog(paramMap);
    }

    public List<MailSendLog> qryMailSendLog(Map paramMap) {
        return this.mailSendLogDao.qryMailSendLog(paramMap);
    }

    public void add(String toAddr, String subject, String content, boolean isHtml) {
        this.add(toAddr, subject, content, null, isHtml);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(String toAddr, String subject, String content, String attachmentPath, boolean isHtml) {
        MailSendLog mailSendLog = new MailSendLog();
        mailSendLog.setPkId(BaseUtil.getPkId());
        mailSendLog.setToAddr(toAddr);
        mailSendLog.setSubject(subject);
        mailSendLog.setContent(content);
        mailSendLog.setAttachmentPath(attachmentPath);
        mailSendLog.setIsHtml((isHtml) ? Const.MAIL_IS_HTML : Const.MAIL_IS_TXT);
        mailSendLog.setSendStatus(Const.MAIL_NOT_SEND);
        mailSendLog.setCrtTs(HdrsDateUtil.getCurrentTime());
        this.mailSendLogDao.add(mailSendLog);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateSendStatus(String pkId, String sendStatus, String sendTime) {
        Map paramMap = new HashMap(16);
        paramMap.put("pkId", pkId);
        paramMap.put("sendStatus", sendStatus);
        paramMap.put("sendTime", sendTime);
        this.mailSendLogDao.updateSendStatus(paramMap);
    }

    /**
     * @Method: delTaskByDate <br>
     * @Param: [map] <br>
     * @Return: void <br>
     * @Description：根据条件删除邮件发送日志表数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/7/15 14:35 <br>
     * @Version： V1.0<br>
     */
    public void delTaskByDate(Map map) {
        mailSendLogDao.delTaskByDate(map);
    }

    /**
     * @Method: reductionMailSendLog <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/7/15 14:43 <br>
     * @Version： V1.0<br>
     */
    public void reductionMailSendLog(String bitchId) {
        mailSendLogDao.reductionMailSendLog(bitchId);
    }
}
